#pragma once
#pragma warning(disable:4996)       // disable checked iterator errors http://msdn.microsoft.com/en-us/library/aa985965(VS.80).aspx 

//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101

#include <Macros.h>
#include <CoVector.h>
#include <CoMatrix.h>
#include <CoCube.h>
#include <ValueHelpers.h>
#include <Settings.h>

#pragma unmanaged 
#include <ql\math\matrix.hpp>
#include <boost/smart_ptr/detail/spinlock.hpp>
#pragma managed 

using namespace System;
using namespace QuantLib;
using namespace Cephei;

#undef HANDLE
#undef ABSTRACT
#define STRUCT
namespace Cephei { namespace QL { namespace Math {
	//////////////////////////////////////////////////////////////////////////////////////////////
	// implementation of IMatrix
	public ref class CMatrix : 
            public System::MarshalByRefObject,
            public Cephei::QL::Math::IMatrix
	{
	protected: 
		boost::shared_ptr<QuantLib::Matrix>* _ppMatrix;
        boost::detail::spinlock* _pSpinlock;
#ifdef HANDLE
		QuantLib::Handle<QuantLib::Matrix>* _phMatrix;
#endif
		Object^ _MatrixOwner;     // reference to object that manages the storage for this object
	internal:
		CMatrix (Cephei::QL::Math::IMatrix^ prm1);
		CMatrix (UInt64 rows, UInt64 columns, Double value);
		CMatrix (UInt64 rows, UInt64 columns);
		CMatrix ();
        CMatrix (boost::shared_ptr<QuantLib::Matrix>& childNative, Object^ owner);
        CMatrix (QuantLib::Matrix& childNative, Object^ owner);
        CMatrix (CMatrix^ copy);
        CMatrix (System::Type^ t);
#ifdef STRUCT
        CMatrix (QuantLib::Matrix childNative);
#endif       
#ifdef HANDLE
		CMatrix (QuantLib::Handle<QuantLib::Matrix>& childNative, Object^ owner);
		CMatrix (QuantLib::Handle<QuantLib::Matrix> childNative);
#endif
		virtual ~CMatrix ();
		!CMatrix ();

	internal:
		QuantLib::Matrix& GetReference ();
		boost::shared_ptr<QuantLib::Matrix>& GetShared ();
		QuantLib::Matrix* GetPointer ();
        void SetMatrix (boost::shared_ptr<QuantLib::Matrix> native)
        {
            if (_ppMatrix != NULL)
                delete _ppMatrix;
            _ppMatrix = new boost::shared_ptr<QuantLib::Matrix> (native);
            
        }
#ifdef HANDLE
		QuantLib::Handle<QuantLib::Matrix>& GetHandle ();
#endif
		virtual bool HasNative () ;
		void Lock ()
		{
			if (Cephei::QL::Settings::_ReferenceLocking)
    			_pSpinlock->lock ();
		}
		void Unlock ()
		{
			_pSpinlock->unlock ();
		}
    public:
		virtual Double At (UInt64 prm1) ;
        property Double Begin 
        {
		    virtual Double get () ;
        }
        property UInt64 Columns 
        {
		    virtual UInt64 get () ;
        }
        property Boolean Empty 
        {
		    virtual Boolean get () ;
        }
        property Double End 
        {
		    virtual Double get () ;
        }
		virtual Double Row_begin (UInt64 i) ;
		virtual Double Row_end (UInt64 i) ;
        property UInt64 Rows 
        {
		    virtual UInt64 get () ;
        }
		virtual Cephei::QL::Math::IMatrix^ Swap (Cephei::QL::Math::IMatrix^ prm1) ;
    };
	//////////////////////////////////////////////////////////////////////////////////////////////
	// implementation of IMatrix Vector interface
	ref class CMatrixVector : public System::MarshalByRefObject,  
							    public INativeVector<IMatrix^>,
							    public INativeVector<Generic::ICell<IMatrix^>^>
	{
	private:
#ifdef STRUCT
		std::vector<QuantLib::Matrix>* _pVValue;
#endif        
		std::vector<boost::shared_ptr<QuantLib::Matrix>>* _pVBoost;
#ifdef HANDLE
		std::vector<QuantLib::Handle<QuantLib::Matrix>>* _pVHand;
#endif
		Object^ _owner;		// object reference to prevent GC of vector owner

	internal:
		CMatrixVector ();
#ifdef STRUCT
		CMatrixVector (std::vector<QuantLib::Matrix>& vec, Object^ owner);
		CMatrixVector (std::vector<QuantLib::Matrix> vec);
#endif       
		CMatrixVector (std::vector<boost::shared_ptr<QuantLib::Matrix>>& vec, Object^ owner);
#ifdef HANDLE
		CMatrixVector (std::vector<QuantLib::Handle<QuantLib::Matrix>>& vec, Object^ owner);
#endif
		virtual ~CMatrixVector ();
		!CMatrixVector ();

    public:
		property int Count 
		{
			virtual int get ();
		}

		property IMatrix^ default[int] 
		{
			virtual IMatrix^ get (int index) = INativeVector<IMatrix^>::default::get;
			virtual void set (int index, IMatrix^ value) = INativeVector<IMatrix^>::default::set;
		}

		//////////////////////////////////////////////////////////////////////////////////////////
		// Interface functions for cell vectors 
	public:
		property Generic::ICell<Cephei::QL::Math::IMatrix^>^ CItem[int] 
		{
			virtual Generic::ICell<IMatrix^>^ get (int index) = INativeVector<Generic::ICell<IMatrix^>^>::default::get;
			virtual void set (int index, Generic::ICell<IMatrix^>^ value) = INativeVector<Generic::ICell<IMatrix^>^>::default::set;
		}
	internal:
#ifdef STRUCT    
		std::vector<QuantLib::Matrix>& GetReference ();
#endif     
		std::vector<boost::shared_ptr<QuantLib::Matrix>>& GetShared ();

#ifdef HANDLE
		std::vector<QuantLib::Handle<QuantLib::Matrix>>& GetHandle ();
#endif
	public:
		virtual bool PrepareFeature (NativeFeature feature);
	};
    
	//////////////////////////////////////////////////////////////////////////////////////////////
	// implementation of IMatrix Matrix interface
	ref class CMatrixMatrix : public System::MarshalByRefObject,  
								public INativeMatrix<IMatrix^>,
							    public INativeMatrix<Generic::ICell<IMatrix^>^>
	{
	private:
#ifdef STRUCT
        std::vector<std::vector<QuantLib::Matrix>>* _pVValue;
#endif       
		std::vector<std::vector<boost::shared_ptr<QuantLib::Matrix>>>* _pVBoost;
#ifdef HANDLE
		std::vector<std::vector<QuantLib::Handle<QuantLib::Matrix>>>* _pVHand;
#endif
		Object^ _owner;		// object reference to prevent GC of vector owner
	internal:
		CMatrixMatrix ();
#ifdef STRUCT        
		CMatrixMatrix (std::vector<std::vector<QuantLib::Matrix>>& vec, Object^ owner);
		CMatrixMatrix (std::vector<std::vector<QuantLib::Matrix>> vec);
#endif        
		CMatrixMatrix (std::vector<std::vector<boost::shared_ptr<QuantLib::Matrix>>>& vec, Object^ owner);
#ifdef HANDLE
		CMatrixMatrix (std::vector<std::vector<QuantLib::Handle<QuantLib::Matrix>>>& vec, Object^ owner);
#endif
		virtual ~CMatrixMatrix ();
		!CMatrixMatrix ();

    public:
		property int Count 
		{
			virtual int get ();
		}

		property Cephei::QL::Math::IMatrix^ default[int, int] 
		{
			virtual IMatrix^ get (int x, int y) = INativeMatrix<IMatrix^>::default::get;
			virtual void set (int x, int y, IMatrix^ value) = INativeMatrix<IMatrix^>::default::set;
		}

		property Generic::ICell<Cephei::QL::Math::IMatrix^>^ CItem[int, int] 
		{
			virtual Generic::ICell<IMatrix^>^ get (int x, int y) = INativeMatrix<Generic::ICell<IMatrix^>^>::default::get;
			virtual void set (int x, int y, Generic::ICell<IMatrix^>^ value) = INativeMatrix<Generic::ICell<IMatrix^>^>::default::set;
		}

	internal:
#ifdef STRUCT
        std::vector<std::vector<QuantLib::Matrix>>& GetReference ();
#endif        
		std::vector<std::vector<boost::shared_ptr<QuantLib::Matrix>>>& GetShared ();

#ifdef HANDLE
		std::vector<std::vector<QuantLib::Handle<QuantLib::Matrix>>>& GetHandle ();
#endif
	public:
		virtual bool PrepareFeature (NativeFeature feature);
	public:
		virtual INativeVector<IMatrix^>^ CreateVector () = INativeMatrix<IMatrix^>::CreateVector;
	public:
		virtual INativeVector<Generic::ICell<IMatrix^>^>^ CreateCellVector () = INativeMatrix<Generic::ICell<IMatrix^>^>::CreateVector;
	};

	//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// Factory class
	public ref class CMatrix_Factory : public System::MarshalByRefObject,  public IMatrix_Factory
	{
	public:
        virtual IMatrix^ Create (Cephei::QL::Math::IMatrix^ prm1);
        virtual IMatrix^ Create (UInt64 rows, UInt64 columns, Double value);
        virtual IMatrix^ Create (UInt64 rows, UInt64 columns);
        virtual IMatrix^ Create ();
        /* <summary> Create a hybrid Matrix of IMatrix, with event notification of changes </summary> */
        virtual Cephei::IMatrix<IMatrix^>^ CreateMatrix();
        /* <summary> Create a hybrid Matrix of ICell of IMatrix, with event notification of changes </summary> */
        virtual Generic::ICell<Cephei::IMatrix<Generic::ICell<IMatrix^>^>^>^ CreateCellMatrix();
        virtual Cephei::IMatrix<IMatrix^>^ CreateMatrix(System::Collections::Generic::IEnumerable<Cephei::IVector<IMatrix^>^>^ source);
        virtual Generic::ICell<Cephei::IMatrix<Generic::ICell<IMatrix^>^>^>^ CreateCellMatrix(System::Collections::Generic::IEnumerable<Cephei::IVector<Generic::ICell<IMatrix^>^>^>^ source);
        /* <summary> Create a hybrid Vector of IMatrix, with event notification of changes </summary> */
        virtual Cephei::IVector<IMatrix^>^ CreateVector();
        /* <summary> Create a hybrid Vector of ICell of IMatrix, with event notification of changes </summary> */
        virtual Generic::ICell<Cephei::IVector<Generic::ICell<IMatrix^>^>^>^ CreateCellVector();
        virtual Cephei::IVector<IMatrix^>^ CreateVector(System::Collections::Generic::IEnumerable<IMatrix^>^ source);
        virtual Generic::ICell<Cephei::IVector<Generic::ICell<IMatrix^>^>^>^ CreateCellVector(System::Collections::Generic::IEnumerable<Generic::ICell<IMatrix^>^>^ source);
    };
   
/*Cephei*/ } /*QL*/ } /*Math */}
